package com.education.welco.controller.teacherapp;

import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtil;
import com.education.welco.pojo.SCatalogWork;
import com.education.welco.service.ISCatalogWorkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: liftsail
 * @Date: 2022/11/30 16:54
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/apptea/CatalogWork")
public class AppTeaCatalogWorkController {

    @Autowired
    private ISCatalogWorkService catalogWorkService;

    //课堂详情-课程考评-作业管理-作业列表  参照/api/welco/sCatalogWork/catalogWorklist
    @GetMapping("/catalogWorklist")
    @ApiOperation("课堂详情-课程考评-作业管理-作业列表")
    @ApiImplicitParam(name = "topCatalogId", value = "课程id", dataType = "Integer")
    public ApiReturnObject catalogWorklist(SCatalogWork catalogWork, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SCatalogWork> sCatalogWorks = catalogWorkService.selectSCatalogWorkAllList(catalogWork);
        PageInfo<SCatalogWork> pageInfo = new PageInfo<>(sCatalogWorks);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    //课堂详情-课程考评-作业管理-发布作业
    @PostMapping("/add")
    public ApiReturnObject addSave(SCatalogWork catalogWork) throws ParseException {
        if (catalogWork.getTopCatalogId() != null && catalogWork.getEndTimeString() != null) {
            catalogWork.setSysState("0");//审核状态
            catalogWork.setCreateTime(new Date());
            //时间区间处理
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isBlank(catalogWork.getAttendTimeString())) {
                catalogWork.setStartTime(DateUtil.getInialDateTime());
            } else {
                catalogWork.setStartTime(format.parse(catalogWork.getAttendTimeString()));
            }
            catalogWork.setEndTime(format.parse(catalogWork.getEndTimeString()));
            catalogWorkService.insertSCatalogWork(catalogWork);
            return new ApiReturnObject("200", "添加成功!", "");
        }
        return new ApiReturnObject("201", "缺少参数!", "");

    }

}
